package com.xszx.business.controller.web;

import com.alibaba.fastjson.JSON;
import com.xszx.server.dto.AjaxJson;
import com.xszx.server.dto.LoginUserDto;
import com.xszx.server.dto.MemberDto;
import com.xszx.server.entity.Member;
import com.xszx.server.entity.User;
import com.xszx.server.exception.BusinessException;
import com.xszx.server.exception.BusinessExceptionCode;
import com.xszx.server.service.MemberService;
import com.xszx.server.util.CopyUtil;
import com.xszx.server.util.UuidUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController("webMemberController")
@RequestMapping("/web/member")
public class MemberController {
    @Resource
    private MemberService memberService;
     @Resource
    private RedisTemplate redisTemplate;

    /**
     * 用户登录
     * @param memberDto
     * @return
     */
    @PostMapping("/login")
    public AjaxJson login(@RequestBody MemberDto memberDto, HttpServletRequest request){
        //从缓冲中获取刚才存储进去的验证信息
        String imageCode = (String) redisTemplate.opsForValue().get(memberDto.getImageCodeToken());
        if(imageCode == null){
           return AjaxJson.error(BusinessExceptionCode.IMAGE_CODE_TOO_FREQUENT.getDesc());
        }else if(!imageCode.toLowerCase().equals(memberDto.getImageCode().toLowerCase())){
            return AjaxJson.error(BusinessExceptionCode.IMAGE_CODE_TOO_FREQUENT.getDesc());
        }
        //进行登录验证
        Member memberDb =memberService.login(CopyUtil.copy(memberDto,Member.class));
        memberDto.setId(memberDb.getId());
        memberDto.setName(memberDb.getName());
        //生成token
        String token = UuidUtil.getShortUuid();
        //设置token属性
        memberDto.setToken(token);
        //将登录成功后的token存储起来
        redisTemplate.opsForValue().set(token, JSON.toJSONString(memberDto), 3600, TimeUnit.SECONDS);
        return AjaxJson.success("登录成功").put("memberDto",memberDto);
    }
    //注册
    @PostMapping("/register")
    public AjaxJson savePassword(@RequestBody Member member){
        member.setPassword(DigestUtils.md5DigestAsHex(member.getPassword().getBytes()));
        int result = memberService.saveOrUpdate(member);
        if(result>0){
            return AjaxJson.success("注册成功");
        }else{
            return AjaxJson.error("注册失败");
        }
    }
    //更新密码
  /*  @PostMapping("/save-password")
    public AjaxJson savePassword(@RequestBody User user){
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        int result = userService.update(user);
        if(result>0){
            return AjaxJson.success("更新成功");
        }else{
            return AjaxJson.error("更新失败");
        }
    }*/
    /**
     * 用户退出登录
     * @param token
     * @return
     */
    @PostMapping("/logout/{token}")
    public AjaxJson logout(@PathVariable("token") String token){
        redisTemplate.delete(token);//将redis中的指定token删除
        return AjaxJson.success("退成登录成功");
    }
}
